// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const app = getApp()

Page({
    data: {
        radio: 'whiteUrl',
        products: [],
        isPreview: false,
        currentIndex: 0,
        urls: [], // 图片数组
        currentImage: ''
    },
    bindViewTap() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },

    bindConcat() {
        console.log("拨打")
        wx.makePhoneCall({
            phoneNumber: '13592325317' //仅为示例，并非真实的电话号码
        })
    },
    previewImage: function (e) {
        console.log('预览')
        var current = e.target.dataset.src;
        var urls = e.target.dataset.urls;
        console.log('预览',current,urls)
        wx.previewImage({
            current: current, // 当前显示图片的http链接  
            urls: urls // 需要预览的图片http链接列表  
        })
        // this.setData({
        //     urls: e.target.dataset.urls
        // });
        // this.openPreview(0)
    },
    onShow() {
        console.log('index')
        this.getTabBar().init();
    },
    onLoad(options) {
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline']
        })
        wx.request({
            url: 'https://wxmini.timefast.top/book/api/qiazi/list',
            success: (res) => {
                res.data.data.forEach(item => {
                    if (item[this.data.radio]) {
                        item.urlArr = item[this.data.radio].split(",");

                        let arrUrlHttps = [];
                        for (let index = 0; index < item.urlArr.length; index++) {
                            const element = item.urlArr[index];
                            arrUrlHttps.push(element.replace("http", "https"));
                        }
                        item.urlArr = arrUrlHttps;
                    }
                });
                res.data.data.sort((a, b) => {
                    return a.sort - b.sort;
                });
                this.setData({
                    products: res.data.data
                })
            }
        })
    },

    onChange(event) {
        this.setData({
            radio: event.detail,
        });

        this.data.products.forEach(item => {
            if (item[this.data.radio]) {
                item.urlArr = item[this.data.radio].split(",");
            }
        });
        this.setData({
            products: this.data.products
        })

    },

    // 打开预览
    openPreview(index) {
        this.setData({
            isPreview: true,
            currentIndex: index,
            currentImage: this.data.urls[index]
        })
    },

    // 箭头切换
    switchImage(e) {
        console.log("切换")
        const dir = e.currentTarget.dataset.dir;
        let newIndex = this.data.currentIndex + (dir === 'next' ? 1 : -1);
        newIndex = Math.max(0, Math.min(newIndex, this.data.urls.length - 1));

        this.setData({
            currentIndex: newIndex,
            currentImage: this.data.urls[newIndex]
        });
    },

    // 手势切换（可选）
    onTouchStart(e) {
        this.startX = e.touches[0].clientX;
    },
    onTouchEnd(e) {
        const deltaX = e.changedTouches[0].clientX - this.startX;
        if (Math.abs(deltaX) > 50) {
            const dir = deltaX > 0 ? 'prev' : 'next';
            this.switchImage({
                currentTarget: {
                    dataset: {
                        dir
                    }
                }
            });
        }
    }
})